// 导入依赖模块
const Mock = require('mockjs2')

const baseURL = window.config.VUE_APP_BASE_URL

const customJson = require('./json/custom.json')
const loginUserJson = require('./json/loginUser.json')
const getNavigatorsByAppNamesJson = require('./json/getNavigatorsByAppNames.json')
const listCustomWidgetBtnByParentIdJson = require('./json/listCustomWidgetBtnByParentId.json')
const getCustomWidgetSqlConfigDTOJson = require('./json/getCustomWidgetSqlConfigDTO.json')
const getSelectOptionJson = require('./json/getSelectOption.json')
const getBusinessItemForm = require('./query/getBusinessItemForm.json')
const listManagedRealmsOfUserJson = require('./uom/listManagedRealmsOfUser.json')

const loginUser = function () {
  return loginUserJson
}

const getNavigatorsByAppNames = function (appNames) {
  if (appNames === '对接管理') {
    return getNavigatorsByAppNamesJson
  }
  return {}
}

const listCustomWidgetBtnByParentId = function (parentId) {
  if (!parentId || !parentId.length) {
    return null
  }
  return listCustomWidgetBtnByParentIdJson.filter(item => item.parentId === parentId)
}

const getCustomWidgetSqlConfigDTO = function (customWidgetId, type) {
  if (!customWidgetId || !customWidgetId.length) {
    return null
  }
  return getCustomWidgetSqlConfigDTOJson.filter(item => item.customWidgetId === customWidgetId)
}

const listManagedRealmsOfUser = function (userId) {
  return listManagedRealmsOfUserJson
}

const getSelectOption = function () {
  return getSelectOptionJson
}

const pageQuery = function (body) {
  if (body.sqlConfigName === 'getBusinessItemForm') {
    return getBusinessItemForm
  }
  return {}
}

const getParams = function (options) {
  const url = new URL(options.url, window.location.origin)
  return Object.fromEntries(url.searchParams.entries())
}

// http://192.168.6.160:8081/dapp-matter/auth/loginUser
Mock.mock(baseURL + 'auth/loginUser1', 'get', loginUser)

// http://192.168.6.160:8081/dapp-matter/privilege/getNavigatorsByAppNames?appNames=
Mock.mock(/\/privilege\/getNavigatorsByAppNames/,
  'get',
  (options) => {
    const params = getParams(options)
    return getNavigatorsByAppNames(params.appNames)
  })

// http://192.168.6.160:8081/dapp-matter/widget/listCustomWidgetBtnByParentId
// ?parentId=402886e28947d02d018953920bae0329&appName=对接管理86&userId=1865
// SELECT * FROM "dc_custom_widgets" where f_parentid = '402886e28947d02d018953920bae0329'
Mock.mock(/\/widget\/listCustomWidgetBtnByParentId/,
  'get',
  (options) => {
    const params = getParams(options)
    return listCustomWidgetBtnByParentId(params.parentId)
  })

// http://192.168.6.160:8081/dapp-matter/widget/getCustomWidgetSqlConfigDTO
// ?customWidgetId=402886e28947d02d018953925eac032a&type=LBCX
Mock.mock(/\/widget\/getCustomWidgetSqlConfigDTO/,
  'get',
  (options) => {
    const params = getParams(options)
    return getCustomWidgetSqlConfigDTO(params.customWidgetId, params.type)[0]
  })

Mock.mock(/\/mock\/api\/select\/option/, 'get', () => { return getSelectOption() })

Mock.mock(/\/sqlQuery\/pageQuery/,
  'post',
  (options) => {
    return pageQuery(JSON.parse(options.body))
  })

Mock.mock(/\/orup\/realm\/listManagedRealmsOfUser/,
  'get',
  (options) => {
    const params = getParams(options)
    return listManagedRealmsOfUser(params.userId)
  })

// 返回一个函数
module.exports = function (app) {
  // 监听 http 请求
  app.get('/form/formtype', function (rep, res) {
    // 设置要返回的数据
    const json = [
      {
        title: '系统表单',
        key: 'system',
        isLeaf: false
      },
      {
        title: '业务表单',
        key: 'business',
        isLeaf: false
      }
    ]
    res.json(Mock.mock(json))
  })
}
